package com.fanhao.dllo.soundfm.fragment.Infofragmnet;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.adapter.InforOneAdapter;
import com.fanhao.dllo.soundfm.entity.InforSubTypeResult;
import com.fanhao.dllo.soundfm.fragment.BaseFragment;
import com.fanhao.dllo.soundfm.utils.Circlelmage;
import com.fanhao.dllo.soundfm.volley.GetBitmap;
import com.fanhao.dllo.soundfm.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 16/1/19.
 * 轮播 type= 9,subType= 1 时的界面
 */
public class InforSubTypeOneFragment extends BaseFragment implements View.OnClickListener {
    private ListView inforOneLv;
    private InforOneAdapter inforOneAdapter;
    private ImageView backInforOneIv;
    private TextView titleHeaderTv, contentHeaderTv, anchorNameHeaderTv;
    private Circlelmage anchorHeaderIv;
    private View headerView;
    private String specialId;

    public InforSubTypeOneFragment(long specialId) {
        this.specialId = String.valueOf(specialId);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_infor_subtype_one;
    }

    @Override
    protected void initView() {

        backInforOneIv = findView(R.id.ivBack_inforSubTypOne);
        backInforOneIv.setOnClickListener(this);
        inforOneLv = findView(R.id.listView_inforOne);
        // 绑定头布局,并初始化
        headerView = LayoutInflater.from(getActivity())
                .inflate(R.layout.fragment_infor_subtype_header, null);
        titleHeaderTv = (TextView) headerView.findViewById(R.id.tvTitle_inforSubType_header);
        contentHeaderTv = (TextView) headerView.findViewById(R.id.tvContent_inforSubType_header);
        anchorNameHeaderTv = (TextView) headerView.findViewById(R.id.tvAnchor_inforSubTypeHeader);
        anchorHeaderIv = (Circlelmage) headerView.findViewById(R.id.ivAnchor_inforSubTypeHeader);


        inforOneLv.addHeaderView(headerView);
        inforOneAdapter = new InforOneAdapter(getActivity());
        inforOneLv.setAdapter(inforOneAdapter);
    }

    @Override
    protected void initData() {
        String url = "http://mobile.ximalaya.com/m/subject_detail?device=android&id=" + specialId;
        final StringRequest request = new StringRequest(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Gson gson = new Gson();
                        InforSubTypeResult result = gson.fromJson(response,
                                InforSubTypeResult.class);
                        if (result != null) {
                            InforSubTypeResult.Info info = result.getInfo();
                            if (info != null) {
                                titleHeaderTv.setText(info.getTitle());
                                contentHeaderTv.setText(info.getIntro());
                                Log.d("InforSubTypeOneFragment", info.getIntro());
                                anchorNameHeaderTv.setText(info.getNickname());
                                GetBitmap.getBitmap(info.getSmallLogo(), anchorHeaderIv);
                            }
                            List<InforSubTypeResult.ListEntity> list = result.getList();
                            if (list != null && list.size() > 0) {
                                inforOneAdapter.addInforSubTypeData(list);
                            }
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        VolleySingleton.getInstance().addRequest(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack_inforSubTypOne:
                goBackToRight();
                break;
        }
    }
}
